package top.mataiji.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mataiji.manager.mapper.BookMapper;
import top.mataiji.manager.pojo.Book;
import top.mataiji.manager.pojo.BookExample;
import top.mataiji.manager.pojo.Bookimg;
import top.mataiji.manager.pojo.vo.BookVo;
import top.mataiji.manager.service.BookImgService;
import top.mataiji.manager.service.BookService;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

/**
 * @Auther: zhaoss
 * @Date: 2021/3/16 - 03 - 16 - 15:39
 * @Description: top.mataiji.manager.service.impl
 * @version: 1.0
 */
@Service
public class BookServiceImpl implements BookService {

    @Autowired
    private BookMapper bookMapper;

    @Autowired
    private BookImgService bookImgService;

    @Override
    public List<Book> findAll() {
        return bookMapper.selectByExample(new BookExample());
    }

    @Override
    public Book findBookById(Integer id) {
        BookExample bookExample = new BookExample();
        bookExample.createCriteria().andBookidEqualTo(id);
        List<Book> books = bookMapper.selectByExample(bookExample);
        if(books.isEmpty()){
            return null;
        }else {
            return books.get(0);
        }
    }

    @Override
    public int deleteBookById(Integer bookId) {
        //创建查询对象
        BookExample bookExample = new BookExample();
        //设置查询参数
        bookExample.createCriteria().andBookidEqualTo(bookId);
        return bookMapper.deleteByExample(bookExample);
    }

    /**
     * 分页查询，
     * @param pageNo 第几也
     * @param pageSize 每页大小
     * @return
     */
    public PageInfo<Book> findAllBypage(int pageNo, int pageSize){
        PageHelper.startPage(pageNo,pageSize);
        List<Book> books = bookMapper.selectByExample(new BookExample());
        PageInfo<Book> bookPageInfo = new PageInfo<>(books);
        return bookPageInfo;
    }

    @Override
    public List<BookVo> findAllBook() {
        //创建返回对象
        List<BookVo> bookVo = new ArrayList<>();
        //查询所有书籍
        List<Book> books = bookMapper.selectByExample(new BookExample());
        //设置书籍的图片
        Iterator<Book> iterator = books.iterator();
        ListIterator<Book> bookListIterator = books.listIterator();
        while (bookListIterator.hasNext()){
            BookVo bookVo1 = new BookVo();
            Book book = bookListIterator.next();
            bookVo1.setBook(book);
            //查询对应bookId的图片
            List<Bookimg> allById = bookImgService.findAllById(book.getBookid());
            bookVo1.setBookimg(allById);
            bookVo.add(bookVo1);
        }
        return bookVo;
    }

    /**
     * 根据id查询详情,详情包括图片，和书籍信息
     * @param bookId
     * @return
     */
    @Override
    public BookVo findBookBiId(Integer bookId) {
        BookVo bookVo = new BookVo();
        //获取书籍图片
        List<Bookimg> allById = bookImgService.findAllById(bookId);
        //获取书籍信息
        //创建查询对象
        BookExample bookExample = new BookExample();
        //设置查询参数
        bookExample.createCriteria().andBookidEqualTo(bookId);
        List<Book> books = bookMapper.selectByExample(bookExample);
//        System.out.println(books.get(0));
        Book book = null;
        if(!books.isEmpty()&&books!=null){
            book = books.get(0);
        }
        //设置book对象
        bookVo.setBook(book);
        //设置图片对象
        bookVo.setBookimg(allById);
        return bookVo;
    }

    @Override
    public int addBook(Book book) {
        Integer bookid = 0;
        if(book.getBookprice()==null){
            book.setBookprice(0L);
        }
        BookExample bookExample = new BookExample();
        bookMapper.insertSelective(book);
        bookExample.createCriteria().andBooknameEqualTo(book.getBookname()).andTypeidEqualTo(book.getTypeid()).andBookpriceEqualTo(book.getBookprice());
        List<Book> books = bookMapper.selectByExample(bookExample);
        if(!books.isEmpty()&&books!=null){
            bookid = books.get(0).getBookid();
        }
        return bookid;
    }

    @Override
    public int alertBook(Book book) {
        return bookMapper.updateByPrimaryKey(book);
    }

    /**
     * 搜索查询
     * @param name
     * @return
     */
    @Override
    public List<BookVo> seleteByBook(String name) {
        System.out.println(name);
        final BookVo[] bookVo = {null};
        List<BookVo> bookVos = new ArrayList<>();
        //创建查询对象
//        BookExample bookExample = new BookExample();
//        bookExample.createCriteria().andBooknameEqualTo();
        List<Book> books = bookMapper.selectByName("\'%"+name+"%\'");
        books.forEach(e->{
            Integer bookid = e.getBookid();
            bookVo[0] = new BookVo();
            bookVo[0].setBook(e);
            bookVo[0].setBookimg(bookImgService.findAllById(bookid));
            bookVos.add(bookVo[0]);
        });
        System.out.println(bookVos+"====");
        return bookVos;
    }

    @Override
    public List<BookVo> selectByType(Integer type) {
        List<BookVo> bookVos = new ArrayList<>();
        System.out.println(type);
        //创建查询对象
        BookExample bookExample = new BookExample();
        bookExample.createCriteria().andTypeidEqualTo(type);
        List<Book> books = bookMapper.selectByExample(bookExample);
        books.forEach(e->{
            BookVo bookVo = new BookVo();
            Integer bookid = e.getBookid();
            bookVo = new BookVo();
            bookVo.setBook(e);
            bookVo.setBookimg(bookImgService.findAllById(bookid));
            bookVos.add(bookVo);
        });
        System.out.println(bookVos+"====");
        return bookVos;
    }


}
